<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>${product.name} - 商品详情</title>
    <style>
        @keyframes fadeIn {
            from {
                opacity: 0;
                transform: translateY(-10px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }
        body {
            margin: 0;
            padding: 0;
            background: linear-gradient(135deg, #f3f4f6, #e2e8f0);
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
        }
        .container {
            max-width: 900px;
            margin: 50px auto;
            background: white;
            border-radius: 20px;
            padding: 30px;
            box-shadow: 0 20px 50px rgba(0, 0, 0, 0.1);
            display: flex;
            gap: 30px;
            animation: fadeInUp 0.6s ease-out forwards;
            opacity: 0;
            transform: translateY(40px);
        }
        .product-img {
            flex: 1;
        }

        .product-img img {
            max-width: 100%;
            border-radius: 15px;
        }

        .product-info {
            flex: 1;
        }

        h2 {
            margin-top: 0;
            font-size: 28px;
            color: #333;
        }

        .price {
            font-size: 22px;
            color: #e74c3c;
            margin: 15px 0;
        }
        .btn-back {
            display: inline-block;
            padding: 10px 20px;
            font-size: 16px;
            font-weight: 500;
            color: white;
            background: linear-gradient(135deg, #c3aed6, #a393eb);
            border: none;
            border-radius: 12px;
            text-decoration: none;
            transition: all 0.3s ease;
            box-shadow: 0 4px 14px rgba(163, 147, 235, 0.3);
            animation: fadeInUp 0.5s ease forwards;
            opacity: 0;
        }

        .btn-back:hover {
            background: linear-gradient(135deg, #a393eb, #c3aed6);
            box-shadow: 0 6px 20px rgba(163, 147, 235, 0.4);
            transform: translateY(-2px);
        }
        /*动画设计*/
@keyframes fadeInUp {
from{
    opacity: 0;
    transform: translateY(20px);
}
to{
    opacity: 1;
    transform: translateY(0);
}

}
.btn{
    padding: 12px 20px;
    background: linear-gradient(to right, #667eea, #764ba2);
    color: white;
    border: none;
    border-radius: 10px;
    font-size: 16px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.3s;
    display: inline-block;
    margin-right: 15px;
}
.btn-hover{
    transform: scale(1.05);
    box-shadow: 0 8px 20px rgba(102, 126, 234, 0.4);

}
@keyframes fadeInUp {
to{
opacity: 1;
transform: translateY(0);
}

}
    </style>
</head>
<body>
<c:if test="${not empty error}">
    <div style="background-color: #fdecea; color: #e74c3c; padding: 15px 25px; margin: 30px auto; width: 90%; max-width: 800px; border-radius: 10px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); animation: fadeIn 0.5s ease-out;">
        ⚠ ${error}
    </div>
</c:if>
<div class="container">
    <div class="product-img">
        <img src="${product.imageUrl}" alt="${product.name}" />
    </div>
    <div class="product-info">
        <h2>${product.name}</h2>
        <p>${product.description}</p>
        <p class="price">原价：￥<span id="unitPrice">${product.price}</span></p>

        <!-- 拼团价信息（如果有拼团活动） -->
        <c:if test="${not empty groupActivity}">
            <p class="price">拼团价：￥<span id="groupPrice">${groupActivity.groupPrice}</span>（${groupActivity.groupSize}人团）</p>
        </c:if>

        <!-- 发起拼团始终显示 -->
        <div style="margin-bottom: 15px;">
            <form action="${pageContext.request.contextPath}/front/group/init" method="post" style="display:inline;">
                <c:choose>
                    <c:when test="${not empty groupActivity}">
                        <input type="hidden" name="activityId" value="${groupActivity.id}">
                    </c:when>
                    <c:otherwise>
                        <input type="hidden" name="productId" value="${product.id}">
                    </c:otherwise>
                </c:choose>
                <button type="submit" class="btn">发起拼团</button>
            </form>
        </div>

        <!-- 立即参团（有拼团活动 + 有未满团） -->
        <c:if test="${not empty groupActivity && not empty existingGroups}">
            <div style="margin-bottom: 20px;">
                <c:forEach var="g" items="${existingGroups}" varStatus="loop">
                    <c:if test="${loop.index == 0}">
                        <a href="${pageContext.request.contextPath}/front/group/detail?id=${groupActivity.id}" class="btn">立即参团</a>
                    </c:if>
                </c:forEach>
            </div>
        </c:if>

        <!-- 加入购物车单独一行 -->
        <div style="margin-bottom: 10px;">
            <form action="${pageContext.request.contextPath}/front/cart/add" method="post" style="display:inline;">
                <input type="hidden" name="productId" value="${product.id}">
                <input type="number" id="quantity" name="quantity" value="1" min="1" style="width:60px; margin-right:8px;">
                <span style="margin-right:12px;">总价：￥<span id="totalPrice">${product.price}</span></span>
                <button type="submit" class="btn">加入购物车</button>
            </form>
        </div>
        <a href="javascript:history.back()" class="btn-back">← 返回</a>
    </div>
</div>
</body>

</body>
<script>
    const quantityInput = document.getElementById('quantity');
    const totalPriceEl = document.getElementById('totalPrice');
    const unitPrice = parseFloat(document.getElementById('unitPrice').innerText);
    quantityInput.addEventListener('input', () => {
        const qty = parseInt(quantityInput.value) || 1;
        totalPriceEl.innerText = (unitPrice * qty).toFixed(2);
    });
</script>
</html>